package com.flashflexpro.tanksinbattle.menuscene;

import com.flashflexpro.tanksinbattle.battlescene.BattleSceneFacade;
import com.flashflexpro.tanksinbattle.menuscene.dto.MenuDto;
import com.flashflexpro.tanksinbattle.menuscene.dto.MenuItemDto;
import com.flashflexpro.tanksinbattle.menuscene.dto.MenuSceneDto;
import com.flashflexpro.tanksinbattle.scene.dto.DependencyDto;
import com.flashflexpro.tanksinbattle.scene.dto.FutureModuleInstDto;
import com.flashflexpro.tanksinbattle.scene.dto.UrlDependencyDto;
import com.flashflexpro.tanksinbattle.utils.DependencyUtil;

import java.io.IOException;

/**
 * A Very typical example that AMF is better than JSON! crossing referencing !!! 
 * @author gyang@brightcove.com
 */
public class MenuSceneFacade {

    public FutureModuleInstDto getFutureMenuScene() throws IOException {
        MenuSceneDto dto = getTempMenuSceneDto();
        FutureModuleInstDto rt = new FutureModuleInstDto();
        rt.setDependencies( new DependencyDto[]{ dto.getDependency() });
        rt.setMain( dto.getDependency().getEntryClass() );
        rt.setDto( DependencyUtil.getAmfBytesFromObject( dto ));
        return rt;
    }

    public MenuSceneDto getTempMenuSceneDto() throws IOException {
        MenuSceneDto rt = new MenuSceneDto();
        rt.setTitle( "Menu from iisfoisi" );
        rt.setTitle( "Menu Scene Title From Server" );

        MenuDto menu1 = new MenuDto();
        menu1.setTitle( "Menu1 From Server" );
        MenuDto menu2 = new MenuDto();
        menu2.setTitle( "Menu2 From Server" );

        MenuItemDto item1 = new MenuItemDto();
        initItem1(menu2, item1);
        MenuItemDto item2 = new MenuItemDto();
        initItemSwitchToBattle(item2);

        menu1.setItems( new MenuItemDto[]{ item1, item2 });

        MenuItemDto item3 = new MenuItemDto();
        initItem3(menu1, item3);
        MenuItemDto item4 = new MenuItemDto();
        initItemSwitchToBattle( item4 );
        menu2.setItems( new MenuItemDto[]{ item3, item4 });

        MenuDto[] menus = new MenuDto[]{ menu1, menu2 };
        rt.setMenus( menus );
        UrlDependencyDto urlD = new UrlDependencyDto();
        urlD.setEntryClass( "com.flashflexpro.tanksinbattle.menuscene.MenuScene" );
        urlD.setSkinSwfUrl( "scene-meue-skin.swf" );
        urlD.setCodeSwfUrl( "scene-menu.swf" );
        rt.setDependency( urlD );
        return rt;
    }

    private void initItem3(MenuDto menu1, MenuItemDto item3) {
        item3.setLabel( "Go to 1st menu" );
        item3.setSwitchToMenu(menu1);
    }

    private void initItem1(MenuDto menu2, MenuItemDto item1) {
        item1.setLabel( "Go to 2nd menu" );
        item1.setSwitchToMenu( menu2 );
    }

    private void initItemSwitchToBattle(MenuItemDto item2) throws IOException {
        item2.setSwitchToModule( new BattleSceneFacade().getBattleScene() );
        item2.setLabel( "Start Playing Now!" );
    }

}
